<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item>
        <el-form-item label="培训名称">
          <el-input
            v-model="formInline.tilite"
            placeholder="培训名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="文档类型">
          <el-select v-model="formInline.document" placeholder="请选择文档类型">
            <el-option label="请选择" value=""></el-option>
            <el-option label="视频教程" value="1"></el-option>
            <el-option label="文档教程" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
        <el-button style="color: green" icon="el-icon-plus" @click="add"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="demo-form-inline" style="text-align: left">
      <el-form-item>
        <el-button
          class="size-icon"
          type="text"
          style="color: green"
          icon="el-icon-s-unfold"
          @click="train"
        ></el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          class="size-icon"
          type="text"
          style="color: green"
          icon="el-icon-menu"
          @click="trainCard"
        ></el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="tilite" label="培训主题" align="center">
        </el-table-column>
        <el-table-column prop="document" label="文档类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.document == 1">文档教程</span>
            <span v-if="scope.row.document == 2">视频教程</span>
          </template>
        </el-table-column>
        <el-table-column prop="learning" label="学习时长" align="center">
        </el-table-column>
        <el-table-column prop="addtime" label="添加时间" align="center">
        </el-table-column>
        <el-table-column prop="study" label="学习人数" align="center">
        </el-table-column>
        <el-table-column prop="unlearned" label="未学习人数" align="center">
        </el-table-column>
        <el-table-column prop="isDelete" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isDelete == true" style="color: green"
              >已发布</span
            >
            <span v-if="scope.row.isDelete == false" style="color: red"
              >已撤销</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: orange"
              v-if="scope.row.isDelete == true"
              @click="examine"
              >查看</el-button
            >
            <el-button
              type="text"
              style="color: red"
              v-if="scope.row.isDelete == true"
              @click="upd"
              >撤销</el-button
            >
            <el-button
              type="text"
              style="color: green"
              v-if="scope.row.isDelete == false"
              @click="upd"
              >发布</el-button
            >
            <el-button
              type="text"
              style="color: red"
              v-if="scope.row.isDelete == false"
              @click="del"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>

    <el-dialog title="状态" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="状态" prop="isDelete">
          <el-input v-model="form.isDelete"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog
      width="600px"
      title="详情信息"
      :visible.sync="dialogFormVisible1"
    >
      <el-form
        ref="form1"
        :model="form1"
        label-width="180px"
        style="width: 600px; margin: 2px auto"
      >
        <el-form-item label="培训标题" prop="isDelete">
          {{ form1.tilite }}
        </el-form-item>
        <el-form-item label="文档类型" prop="isDelete">
          {{ form1.document }}
        </el-form-item>
        <el-form-item label="学习时长" prop="isDelete">
          {{ form1.learning }}
        </el-form-item>
        <el-form-item label="封面照片" prop="isDelete">
          {{ form1.url }}
        </el-form-item>
        <el-form-item label="上传文件" prop="isDelete">
          {{ form1.uploading }}
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  GetShowTrain,
  deleteTrain,
  updateTrain,
  GetTotaocount,
} from "@/api/SafeManage/Train";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      multipleSelection: [],
      ids: {},
      form: {
        isDelete: "",
      },
      form1: {
        tilite: "",
        document: "",
        learning: "",
        url: "1",
        uploading: "入园学习.PDF",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        tilite: "",
        document: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //显示
    list() {
      GetShowTrain(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deleteTrain(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //修改
    upd() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.dialogFormVisible = true;
      this.multipleSelection.forEach((res) => {
        this.form = res;
      });
    },
    baocun() {
      updateTrain(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //查看
    examine() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.dialogFormVisible1 = true;
      this.multipleSelection.forEach((res) => {
        this.form1 = res;
      });
    },
    //跳转
    add() {
      this.$router.push("/SafeManage/AddTrain");
    },
    //列表
    train() {
      this.$router.push("/SafeManage/Train");
    },
    //卡片
    trainCard() {
      this.$router.push("/SafeManage/TrainCard");
    },
  },
};
</script>
<style>
.size-icon {
  font-size: 30px;
}
</style>